package com.cy.servlet;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cy.bean.AdminList;
import com.cy.bean.UserList;
import com.cy.service.LoginService;
import com.cy.service.LoginServiceImpl;
import com.cy.util.CookieUtil;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet({ "/LoginServlet", "/login" })
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置前端页面传递回来的字符编码
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");

		// 获得登录用户级别
		String judge = request.getParameter("judge");

		// 获取用户名密码/是否记住密码
		String remember = request.getParameter("remember");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		LoginService loginService = new LoginServiceImpl();
		request.getSession().setAttribute("judge", judge);
		if (judge != null && judge.equals("0")) {
			// 登录到普通用户
			UserList gengraluser = new UserList();
			gengraluser = loginService.findUser(username, password);
			if (gengraluser == null) {
				// 退出到登录页面
				request.setAttribute("error", "用户名/密码错误");
				request.getRequestDispatcher("index.jsp").forward(request, response);
			} else if (gengraluser != null) {
				// 登录成功
				// 保存用户信息,有效时间为120分钟
				request.getSession().setAttribute("nowuser", gengraluser.getUsername());
				request.getSession().setAttribute("gengraluser", gengraluser);
				// 记住密码，记住一个礼拜
				if (remember != null && remember.equals("on")) {
					CookieUtil.addCookie("cookie_name_pass_judge", 7 * 24 * 60 * 60, request, response,
							URLEncoder.encode(username, "utf-8"), URLEncoder.encode(password, "utf-8"),URLEncoder.encode(judge, "utf-8"));
				}
				request.getRequestDispatcher("buygame").forward(request, response);
			}
		} else if (judge != null && judge.equals("1")) {
			// 登录到超级管理员
			AdminList adminuser = new AdminList();
			adminuser = loginService.findAdmin(username, password);
			if (adminuser == null) {
				// 退出到登录页面
				request.setAttribute("error", "用户名/密码错误");
				request.getRequestDispatcher("index.jsp").forward(request, response);
			} else if (adminuser != null) {
				// 登录成功
				// 保存用户信息,有效时间为120分钟
				request.getSession().setAttribute("nowuser", adminuser.getUsername());
				request.getSession().setAttribute("adminuser", adminuser);
				// 记住密码，记住一个礼拜
				if (remember != null && remember.equals("on")) {
					CookieUtil.addCookie("cookie_name_pass_judge", 7 * 24 * 60 * 60, request, response,
							URLEncoder.encode(username, "utf-8"), URLEncoder.encode(password, "utf-8"),URLEncoder.encode(judge, "utf-8"));
				}
				request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response);
			}
		}
	}

}
